import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Solution1110 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
        Set<Integer> set=new HashSet<>();
        for (int i : to_delete) {
            set.add(i);
        }
        List<TreeNode> list=new ArrayList<>();
        dfs(set,list,null,root);
        if (!set.contains(root.val)){
            list.add(root);
        }
        return list;
    }

    private void dfs(Set<Integer> set, List<TreeNode> list, TreeNode p, TreeNode root) {
        if (root==null){
            return;
        }
        dfs(set,list,root,root.left);
        dfs(set,list,root,root.right);
        if (set.contains(root.val)){
            if (p!=null && p.left==root){
                p.left=null;
            }
            if (p!=null && p.right==root){
                p.right=null;
            }
            if (root.left!=null){
                list.add(root.left);
            }
            if (root.right!=null){
                list.add(root.right);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution1110().delNodes(new TreeNode(1,new TreeNode(2,new TreeNode(4),new TreeNode(5)),new TreeNode(3,new TreeNode(6),new TreeNode(7))),new int[]{3,5}).size());
    }
}
